﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace LinqToPowershell
{
    /// <summary>
    /// An EagerQueryable is a QueryableWrapper that also
    /// (a) actually implements GetEnumerable() instead of just wrapping it
    /// (b) tells the compiler it IS-AN IQueryable so that Powershell evaluates it eagerly
    /// Note: since we can't multiply-inherit from QueryableWrapper(T) and EagerQueryable, this generic version has to duplicate some properties
    /// </summary>
    public class EagerQueryable<T> : EagerQueryable, IQueryableWrapper<T>, IQueryable<T>
    {
        public EagerQueryable(IQueryableWrapper<T> wrapper)
            : base(wrapper)
        {
            GetEnumeratorFunc = wrapper.GetEnumeratorFunc;
        }

        public new IEnumerator<T> GetEnumerator() { return GetEnumeratorFunc(); }

        // same as QueryableWrapper<T>::Query -- though as long as we have to write our own, may as well remove unnecessarily recursive construction
        public new IQueryable<T> Query { get { return this; } }

        // same as QueryableWrapper<T>::GetEnumeratorFunc
        public new Func<IEnumerator<T>> GetEnumeratorFunc { get; protected set; }
    }

    /// <summary>
    /// An EagerQueryable is a QueryableWrapper that also
    /// (a) actually implements GetEnumerable() instead of just wrapping it
    /// (b) tells the compiler it IS-AN IQueryable so that Powershell evaluates it eagerly
    /// </summary>
    public class EagerQueryable : QueryableWrapper, IQueryableWrapper, IQueryable
    {
        public EagerQueryable(IQueryableWrapper wrapper)
            : base(wrapper)
        { }

        public IEnumerator GetEnumerator() { return GetEnumeratorFunc(); }
    }
}
